#include "buton.h"
#include <stdio.h>


buton::buton(GLdouble xt,GLdouble yt , GLdouble wt,GLdouble ht,int statet , int hightlightedt,char *labelt, ApelFunctie Apelt)
{
	x = xt;
	y = yt;
	w = wt;
	h = ht;
	state = statet;
	highlighted = hightlightedt;
	label = new char[20];
	comm = new char [20];
	strcpy(label , labelt);
	strcpy(comm , labelt);
	Apel = Apelt;
};

bool buton::clicked(GLdouble xt,GLdouble yt)
{
	if( xt > x &&
		xt < x+w &&
		yt > y &&
		yt < y+h ) 
		return true;
	return false;		
};

void buton::release(GLdouble xt,GLdouble yt, GLdouble mousex,GLdouble mousey)
{
	if ( clicked(xt,yt) &&  Apel){
		Apel(comm);
	}//clicked(mousex,mousey) &&
	//printf("%d",clicked(mousex,mousey));
	state = 0;
};

void buton::press(GLdouble xt , GLdouble yt)
{
	if(clicked(xt,yt))
		state = 1;
};

bool buton::pasiv(GLdouble xt , GLdouble yt, GLdouble mousex,GLdouble mousey)
{
	if(clicked(xt,yt))
	{
		if(highlighted == 0)
		{
			highlighted = 1;
			glutPostRedisplay();
			return true;
		}

	}
	else
	{		
		highlighted = 0;
		glutPostRedisplay();
	}
	return false;
};

void buton::draw()
{
	if (highlighted) 
		glColor3f(0.7f,0.8f,0.8f);
	else 
		glColor3f(0.6f,0.6f,0.6f);

	glBegin(GL_QUADS);
	glVertex2f( x , y );
	glVertex2f( x , y+h );
	glVertex2f( x+w, y+h );
	glVertex2f( x+w, y );
	glEnd();

	glLineWidth(3);
	if (state) 
		glColor3f(0.4f,0.4f,0.4f);
	else 
		glColor3f(0.8f,0.8f,0.8f);

	glBegin(GL_LINE_STRIP);
	glVertex2f( x+w, y );
	glVertex2f( x , y );
	glVertex2f( x , y+h );
	glEnd();

	if (state) 
		glColor3f(0.8f,0.8f,0.8f);
	else 
		glColor3f(0.4f,0.4f,0.4f);

	glBegin(GL_LINE_STRIP);
	glVertex2f( x , y+h );
	glVertex2f( x+w, y+h );
	glVertex2f( x+w, y );
	glEnd();

	glLineWidth(3);

	GLdouble fontx = x + (w - (GLdouble)glutBitmapLength(GLUT_BITMAP_HELVETICA_12,(const unsigned char *)label)/glutGet(GLUT_WINDOW_WIDTH)) / 2 ;
	GLdouble fonty = y + (h-(GLdouble)12/glutGet(GLUT_WINDOW_HEIGHT))/2;

	if (state) {
		fontx+=0.001F;
		fonty+=0.001F;
	}

	if(highlighted)
	{
		glColor3f(1.0,1.0,1.0);
		glRasterPos2f(fontx, fonty);

		for (int i = 0 ; i < strlen(label) ; i++)
		{
			glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, label[i] );
		}
		fontx-=0.001F;
		fonty-=0.001F;
	}
	else{
		glColor3f(0.1,0.1,0.1);
		glRasterPos2f(fontx, fonty);

		for (int i = 0 ; i < strlen(label) ; i++)
		{
			glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, label[i] );
		}
	}
};
void buton::Setcomm(char *te)
	{
		strcpy(comm,te);
};

buton::~buton(void)
{
	delete label;
};
